package com.tianjian.controller;

import com.tianjian.dto.WordInfo;
import com.tianjian.entity.IkExtWordEntity;
import com.tianjian.exception.SelfException;
import com.tianjian.service.IkExtWordService;
import com.tianjian.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@RestController
public class IKAnalyzerController {
    @Autowired
    private IkExtWordService ikExtWordService;

    /**
     * 获取所有分词
     *
     * @return
     */
    @GetMapping(value = "/ik/search", produces = {"text/plain;charset=utf-8"})
    public String searchByIKPlugin() {
        log.info("获取最新分词词典");
        StringJoiner stringJoiner = new StringJoiner(StringUtils.LF);
        List<IkExtWordEntity> list = ikExtWordService.allWorld();
        if (CollectionUtils.isEmpty(list)) {
            return StringUtils.EMPTY;
        }
        list.forEach(e -> stringJoiner.add(e.getWord()));
        return stringJoiner.toString();
    }

    /**
     * 检测是否需要请求分词
     *
     * @param response
     */
    @RequestMapping(value = "/ik/search", method = RequestMethod.HEAD)
    public void headAllHotWord(HttpServletResponse response) {
        log.info("检测是否需要更新分词词典");
        String eTag = ikExtWordService.getETag();
        response.setHeader("ETag", eTag);
    }

    /**
     * 添加分词
     *
     * @param word
     * @return
     */
    @PostMapping("/extDict/add")
    public ApiResult addWord(@RequestParam("word") String word) {
        try {
            return ApiResult.ok(ikExtWordService.addWord(word));
        } catch (ServiceException e) {
            throw new SelfException(e.getMessage());
        }
    }

    @GetMapping("/ik/findAll")
    @CrossOrigin
    public ApiResult findAll(WordInfo wordInfo) {
        Page<IkExtWordEntity> ikExtWordEntities = ikExtWordService.findAllByWordLike(wordInfo);
        return ApiResult.ok(ikExtWordEntities.getContent()).put("total", ikExtWordEntities.getTotalElements());
    }

    /**
     * 删除指定分词
     *
     * @param id
     */
    @DeleteMapping("/extDict/delete/{id}")
    public ApiResult deleteWord(@PathVariable("id") Long id) {
        ikExtWordService.deleteWord(id);
        return ApiResult.ok();
    }

    /**
     * 条件查询
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/extDict/search")
    public ApiResult findBySearch(String search, int page, int size) {
        Page<IkExtWordEntity> bySearchKey = ikExtWordService.findBySearchKey(search, page, size);
        return ApiResult.ok(bySearchKey.getContent()).put("total", bySearchKey.getTotalElements());
    }

    /**
     * 更新指定分词
     *
     * @param wordEntity
     * @return
     */
    @PutMapping("/extDict/update")
    public ApiResult updateWord(@RequestBody IkExtWordEntity wordEntity) {
        try {
            return ApiResult.ok(ikExtWordService.updateExtWord(wordEntity));
        } catch (ServiceException e) {
            throw new SelfException(e.getMessage());
        }
    }
}
